package com.example.flutter_ad_demo

import android.content.Intent
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import androidx.annotation.NonNull
import com.bwt.top.AdSdk

class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        InitManager.instance.init(this)
        // 其他的使用自己的广告SDK
        if (InitManager.instance.bwtInited) {
            if (InitManager.instance.bwtInitSuccess) {
                jumpSplashActivity()
            }
        } else {
            InitManager.instance.bwtInitCallback = AdSdk.InitCallback { initSuccess ->
                if (initSuccess) {
                    jumpSplashActivity()
                }
            }
        }
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        val channel =
            MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "native_controller_channel")
        //设置dart调用kotlin的函数监听
        channel.setMethodCallHandler { call, result ->
            if (call.method == "showSplashAd") {
                jumpSplashActivity()
                result.success(true)
            } else if (call.method == "showInterstitialAd") {
                showInterstitialAd()
                result.success(true)
            }
        }
    }

    private fun jumpSplashActivity() {
        startActivity(Intent(this, SplashAdActivity::class.java))
    }

    private fun showInterstitialAd() {
        InterstitialAdManager.instance.showInterstitialAd(this)
    }
}
